package com.lyxs916.appbaselibrary.core.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.lyxs916.appbaselibrary.core.iview.IActivityAndFragment;
import com.lyxs916.corelib.R;

import butterknife.ButterKnife;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * 说明:
 * 作者： 杨阳; 创建于：  2017-06-06  13:54
 */
public abstract class BaseActivity extends AppCompatActivity implements IActivityAndFragment {
    /**
     * 说明:请求code;
     * 注：%60000是因为请求的code最大不超过65535
     * 作者： 杨阳; 创建于：  2017-06-01  17:34
     */
    public int REQUEST_CODE = Math.abs(this.getClass().getSimpleName().hashCode() % 60000);
    public BaseActivity mActivity;
    /**
     * 订阅的集合，防止内存泄漏
     */
    private CompositeSubscription mCompositeSubscription;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setContentView());
    }


    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
        mActivity = this;
        parseIntent(getIntent());
        initView();

    }

    /**
     * 获取布局id
     *
     * @return Layout 地址
     */
    public abstract int setContentView();

    /**
     * 解析意图
     *
     * @param intent
     */
    public abstract void parseIntent(Intent intent);

    /**
     * 初始化,用于规范代码
     */
    public abstract void initView();

    /**
     * @return 获取需要转化为loadingAndRetryManager的控件
     */
    public View getSwitchRoot() {
        return findViewById(R.id.switchRootId);
    }


    /**
     * 添加一个订阅
     *
     * @param subscription
     */
    @Override
    public void addSubscription(Subscription subscription) {
        if (this.mCompositeSubscription == null) {
            this.mCompositeSubscription = new CompositeSubscription();
        }
        this.mCompositeSubscription.add(subscription);
    }

    /**
     * @return RXJava 的订阅集合  用于销毁
     */
    @Override
    public CompositeSubscription getCompositeSubscription() {
        if (this.mCompositeSubscription == null) {
            this.mCompositeSubscription = new CompositeSubscription();
        }
        return this.mCompositeSubscription;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        /**
         * 销毁网络访问的订阅
         */
        if (this.mCompositeSubscription != null) {
            this.mCompositeSubscription.unsubscribe();
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        /**
         * 销毁网络访问的订阅
         */
        if (this.mCompositeSubscription != null) {
            this.mCompositeSubscription.unsubscribe();
        }
    }

    public BaseActivity getBaseActivity() {
        return mActivity;
    }

}
